<?php

namespace app\logistics\validate;

use think\Validate;
use think\facade\Config;

/** 
 * 物流规则验证器
 * @author huangfeng
 * @date 2024-7-3
*/
class LogisticsRules extends Validate
{
    /** 
     * 定义验证规则
     * @var array '字段名' =>  ['规则1','规则2'...]
     * 
    */
    protected $rule = [
        'rule_name'     => 'require|max:255',
        'logistics_name'     => 'require|checkDict:d_logistics',
        'terms'     => 'in:00,01',
        'freight_tag'     => 'in:L,S,H,T',
        'from_nation'     => 'checkDict:d_nation',
        'to_nation'     => 'checkDict:d_nation',
        'model'     => 'in:SM,MD,LG',
        'weight_lower'     => 'float',
        'weight_upper'     => 'float',
        'transfer_fee'     => 'float',
        'single_fee'     => 'float',
        'magnetic_inspection_fee'     => 'float',
        'customs_declaration_fee'     => 'float',
        'insurance_rate'     => 'float',
        '1insurance_fee'     => 'float',
        'freight'     => 'require|float',
        'level'     => 'require|number',
        'is_active'     => 'in:0,1',
        'notes'     => 'max:255',
        'id'     => 'require|number|gt:0',
        'page'     => 'integer|gt:0',
        'pagesize'     => 'integer|between:1,100',
    ];

    /** 
     * 定义验证场景
     * @var array '场景名' =>  ['字段名1','字段名2'...]
    */
    protected $scene = [
        'add' => ['rule_name','logistics_name','terms','freight_tag','from_nation','to_nation','model','weight_lower','weight_upper','transfer_fee','single_fee','magnetic_inspection_fee','customs_declaration_fee','insurance_rate','1insurance_fee','freight','level','is_active','notes'],
        'update' => ['id','rule_name','logistics_name','terms','freight_tag','from_nation','to_nation','model','weight_lower','weight_upper','transfer_fee','single_fee','magnetic_inspection_fee','customs_declaration_fee','insurance_rate','1insurance_fee','freight','level','is_active','notes'],
        'info' => ['id'],
        'delete' => ['id'],
        'list' => ['page','pagesize'],
        'active'=> ['logistics_name','terms'],
    ];

    /** 
     * 定义错误消息
     * @var array '字段名.规则名' =>  '错误信息'
    */
    protected $message = [
        'rule_name.require' => ['code'=>'20001','message'=>'请输入规则名称'],
        'rule_name.max' => ['code'=>'20002','message'=>'规则名称最多不能超过255个字符'],
        'logistics_name.require' => ['code'=>'20003','message'=>'请选择物流公司'],
        'logistics_name.checkDict' => ['code'=>'20004','message'=>'请选择正确的物流公司'],
        'terms.in' => ['code'=>'20005','message'=>'贸易条款必须是DDP或DDU)'],
        'freight_tag.in' => ['code'=>'20006','message'=>'运费标记必须是L,S,H,T'],
        'form_name.checkDict' => ['code'=>'20007','message'=>'请选择正确的始发国家'],
        'to_nation.checkDict' => ['code'=>'20008','message'=>'请选择正确的目标国家'],
        'model.in' => ['code'=>'20009','message'=>'机型必须是SM,MD,LG'],
        'weight_lower.float' => ['code'=>'20010','message'=>'最小重量必须是数字'],
        'weight_upper.float' => ['code'=>'20011','message'=>'最大重量必须是数字'],
        'transfer_fee.float' => ['code'=>'20012','message'=>'调拨费必须是数字'],
        'single_fee.float' => ['code'=>'20013','message'=>'单件费必须是数字'],
        'magnetic_inspection_fee.float' => ['code'=>'20014','message'=>'磁检费必须是数字'],
        'customs_declaration_fee.float' => ['code'=>'20015','message'=>'报关费必须是数字'],
        'insurance_rate.float' => ['code'=>'20016','message'=>'保率必须是数字'],
        '1insurance_fee.float' => ['code'=>'20017','message'=>'保险费必须是数字'],
        'level.require' => ['code'=>'20018','message'=>'请输入规则级别'],
        'level.number' => ['code'=>'20019','message'=>'级别必须是整数'],
        'is_active.in' => ['code'=>'20020','message'=>'是否激活必须是0或1'],
        'notes.max' => ['code'=>'20021','message'=>'备注最多不能超过255个字符'],
        'freight.require' => ['code'=>'20022','message'=>'请输入运费'],
        'freight.float' => ['code'=>'20023','message'=>'运费必须是数字'],
        'id.require' => ['code'=>'20024','message'=>'请输入物流规则ID'],
        'id.number' => ['code'=>'20025','message'=>'物流规则ID必须是数字'],
        'page.integer' => ['code'=>'20026','message'=>'页码必须是整数'],
        'page.gt' => ['code'=>'20027','message'=>'页码最小为1'],
        'pagesize.integer' => ['code'=>'20028','message'=>'每页条数数必须是整数'],
        'pagesize.between' => ['code'=>'20030','message'=>'每页条数为1-100之间'],
        'id.gt' => ['code'=>'20031','message'=>'物流规则ID必须大于0'],
    ];

    /**
     * 自定义数据字典验证规则
     * @param string $value:字段值
     * @param string $rule:验证数据字典名称
     * @param array $data:全部数据
     * @return bool
    */
    protected function checkDict($value, $rule, $data)
    {
        $DD_PATH = Config::get('app.datadict_path');
        if (file_exists($DD_PATH.'/cn/' . $rule . '.php')){
            $arrDict = include $DD_PATH.'/cn/' . $rule . '.php';
            if (is_array($arrDict)){
                if (isset($arrDict[$value])){
                    return true;
                }
            }
        }
        return false;
    }
}
?>